package com.haier.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haier.constant.HpConstant;
import com.haier.constant.URLConstant;
import com.haier.entity.HpToken;
import com.haier.mapper.HpTokenMapper;
import com.haier.service.IHpTokenService;
import com.haier.utils.H5Util;
import com.haier.utils.HttpUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author luanxuecheng
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class HpTokenServiceImpl extends ServiceImpl<HpTokenMapper, HpToken> implements IHpTokenService {

	@Override
	public String getToken() throws Exception {
		Long id = HpConstant.TOKEN_ID;
		HpToken hpToken = baseMapper.selectById(id);
		Long thisTime = System.currentTimeMillis();
		if (hpToken == null) {
			hpToken = new HpToken();
			String token = H5Util.getToken();
			hpToken.setId(id);
			hpToken.setToken(token);
			hpToken.setExpirationTime(new Date(thisTime + 7200000L));
			baseMapper.insert(hpToken);
			return token;
		}
		if (hpToken.getExpirationTime().getTime() - thisTime < 600000L) {// 如果小于10分钟 重新拉取token
			String token = H5Util.getToken();
			hpToken.setToken(token);
			hpToken.setExpirationTime(new Date(thisTime + 7200000L));
			baseMapper.updateById(hpToken);
			return token;
		}
		return hpToken.getToken();
	}

	@Override
	public String getHaierToken() throws Exception {
		Long id = HpConstant.HAIER_TOKEN_ID;
		HpToken hpToken = baseMapper.selectById(id);
		Long thisTime = System.currentTimeMillis();
		if (hpToken == null) {
			JSONObject token = getHaierTokenJson();
			String haierToken = token.getString("access_token");
			hpToken = new HpToken();
			hpToken.setId(id);
			hpToken.setToken(haierToken);
			Long expirationTime = thisTime + token.getLongValue("expires_in") * 1000;
			hpToken.setExpirationTime(new Date(expirationTime));
			baseMapper.insert(hpToken);
			return haierToken;
		}
		if (hpToken.getExpirationTime().getTime() - thisTime < 600000L) {// 如果小于10分钟 重新拉取token
			JSONObject token = getHaierTokenJson();
			String haierToken = token.getString("access_token");
			hpToken.setToken(haierToken);
			Long expirationTime = thisTime + token.getLongValue("expires_in") * 1000;
			hpToken.setExpirationTime(new Date(expirationTime));
			hpToken.setToken(haierToken);
			baseMapper.updateById(hpToken);
			return haierToken;
		}
		return hpToken.getToken();
	}

	private JSONObject getHaierTokenJson() throws Exception {
		Map<String, String> header = new HashMap<String, String>();
		header.put("Authorization", "Basic UzAwNzQ2OlMwMDc0Ng==");
		header.put("TENANT_ID", "1");
		String tokenResult = HttpUtils.post(URLConstant.URL_GET_AUTHORIZATION, header);
		JSONObject token = JSON.parseObject(tokenResult);
		return token;
	}
}
